//
//  IJSImageGet.m
//  IJSFramework
//
//  Created by shange on 2017/4/12.
//  Copyright © 2017年 jinshan. All rights reserved.
//

#import "IJSImageGet.h"


@implementation IJSImageGet

+ (UIImage *)loadImageWithBundle:(NSString *)bundleName subFile:(NSString *)subFile grandson:(NSString *)grandson imageName:(NSString *)imageName imageType:(NSString *)imageType
{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:bundleName ofType:@"bundle"];
    if (!bundlePath) {
        NSLog(@"请正确填写bundle的名字");
        return nil;
    }
    if (subFile) {
        NSString *filePath = [bundlePath stringByAppendingPathComponent:subFile];
        if (grandson) {
            NSString *lastFilePath = [filePath stringByAppendingPathComponent:grandson];
            NSString *imagePath = [[NSBundle bundleWithPath:lastFilePath] pathForResource:imageName ofType:imageType];
            if (!imagePath) {
                NSLog(@"根据传入的底层文件夹目录获取不到路径,请检查资源名字或者子文件路径");
                return nil;
            }
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imagePath]];
            return [UIImage imageWithData:imageData];
        }
        NSString *imagePath = [[NSBundle bundleWithPath:filePath] pathForResource:imageName ofType:imageType];
        if (!imagePath) {
            NSLog(@"根据传入的子文件目录获取不到路径,请检查资源名字或者子文件路径");
            return nil;
        }
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imagePath]];
        return [UIImage imageWithData:imageData];
    } else {
        NSString *imagePath = [[NSBundle bundleWithPath:bundlePath] pathForResource:imageName ofType:imageType];
        if (!imagePath) {
            NSLog(@"根据传入的参数获取不到路径,请检查资源名字");
            return nil;
        } else {
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imagePath]];
            return [UIImage imageWithData:imageData];
        }
    }
    return nil;
}


@end
